<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserModel
 *
 * @author wangnaixin
 */

namespace Admin\Model;

use Think\Model\RelationModel;
use Common\Lib\Util\LogUtil;
class UserModel extends RelationModel {
    //put your code here
    
    protected $tableName = 'users'; 
    
    protected $_auto = array (
        array('password','md5',3,'function') ,
        array('create_time','time',1,'function')
    );
    
    protected $_link = array(
        'role'=>array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'role',
            'mapping_name'=>'role',
            'foreign_key'=>'role_id',
            'parent_key'  => 'id'
        ),
        'auth'=>array(
            'mapping_type'      =>  self::MANY_TO_MANY,
            'class_name'        =>  'auth',
            'mapping_name'      =>  'auths',
            'foreign_key'       =>  'role_id',
            'mapping_key'       => 'role_id',
            'relation_foreign_key'  =>  'auth_id',
            'relation_table'    =>  'role_auth'
        )
        
    );
    public function doLogin($userName,$passWord){
        $where = array(
            'user_name' => array('eq',$userName),
            'password' => array('eq',  md5($passWord))
        );
        $userInfo = $this->relation(true)->where($where)->find();
        if(!empty($userInfo) && $userInfo !== false){
            //返回用户基本信息
            return $userInfo;
        }else{
            return false;
        }
        
    }
    
    public function resetUserPass($userId,$password=123456){
        $data = array(
            'password' => md5($password)
        );
        return $this->where("id = ".$userId)->data($data)->save();
    }
    
    public function doLoginById($userId){
        $where = array(
            'id' => array('eq',$userId)
        );
        $userInfo = $this->relation(true)->where($where)->find();
        if(!empty($userInfo) && $userInfo !== false){
            //返回用户基本信息
            return $userInfo;
        }else{
            return false;
        }
    }

    /**
     * @todo  按照用户组获取管理员用户列表
     * @param $roleIds
     * @param array $append
     * @return mixed
     */
    public function getAdminList($roleIds,$append=array()){
        $where = array('role_id'=>array('in',$roleIds));
        if ($append) {
            $where = array_merge($where,$append);
        }
        return $this->where($where)->select();
    }
    
    /**
     * @todo 根据搜索条件返回用户列表
     * 用户姓名,联系电话,角色,年龄,性别
     */
    
    public function getUserList($where=[]){
        $count = $this->where($where)->count();
        $p = getPage($count,15);
        $list = $this->relation(true)->field(true)->where($where)->order('id')->limit($p->firstRow, $p->listRows)->select();
        
        return array('list'=>$list,'page'=>$p->show());
    }
    
    /**
     * @todo  禁用
     * @param type $userId
     * @return type
     */
    public function deUserById($userId){
        $data = array(
            'status'=>0
        );
        return $this->where("id = ".$userId)->data($data)->save();
    }
    
    
    /**
     * @todo  启用
     * @param type $userId
     * @return type
     */
    public function enUserById($userId){
        $data = array(
            'status'=>1
        );
        return $this->where("id = ".$userId)->data($data)->save();
    }

    public function getUserInfoById($userId){
        return $this->relation(array('role','attachment'))->where("id = ".$userId)->find();
    }
    
    public function getUserInfoByName($userName,$userPwd){
    	$name = sprintf('=\'%s\' or mobile=\'%s\'',$userName,$userName);
    	$where = array('user_name'=>array('exp',$name));
    	$map['_complex'] = $where;
    	$map['password'] = md5($userPwd);
    	return $this->relation(array('role','attachment'))->where($map)->find();
    }
    
    public function checkUserExistByName($userName){
    	return $this->where(array('user_name'=>$userName))->count();
    }
    
    public function checkUserExistByMobile($mobile){
    	return $this->where(array('mobile'=>$mobile))->count();
    }

    public function getUserByTeamId($teamId) {
        $userIds = array();
        if ($teamId != 0) {
            $where = array();
            if ($teamId)  {
                $where['team_id'] = array('in',$teamId);
            } else {
                $where['team_id'] = 0;
            }
            $users = $this->where($where)->select();
            foreach ($users as $user) {
                $userIds[] = $user['id'];
            }
        }
        return $userIds;
    }

    /**
     * 检查用户名|手机号|身份证号是否有重复
     * @param $fieldName 待检查的字段名称 user_name|mobile|identity
     * @param $filedValue
     * @param $id
     * @return bool
     */
    public function checkExists($fieldName,$filedValue,$id=0) {
        if (!$filedValue) {
            return false;
        }
        $fields = array(
            $fieldName => $filedValue
        );
        $pk = $id ? array('id' => $id) : array();
        return $this->isExists($this->tableName,$fields,$pk);
    }

    
}
